﻿using System.Windows.Forms;
using System;
using System.ComponentModel;
using Mbs.ComponentModel.Container;

namespace Mbs.Windows
{
    /// <summary>
    /// Shell Form Presenter
    /// </summary>
    /// <typeparam name="TShell"></typeparam>
    public class ShellPresenter<TShell> : Presenter, IShellPresenter, ISynchronizeInvoke
        where TShell : FormView
    {
       
        /// <summary>
        /// Shell 视图
        /// </summary>
        [Inject]
        public virtual new TShell View
        {
            get
            {
                return (TShell)base.View;
            }
            set
            {
                AttachView(value);
            }
        }

        /// <summary>
        /// Shell 视图
        /// </summary>
        Form IShellPresenter.View
        {
            get { return View; }
        }

        #region ISynchronizeInvoke Members
        private ISynchronizeInvoke HWND
        {
            get
            {
                return SynchronizeService.SynchronizeObject;
            }
        }

        IAsyncResult ISynchronizeInvoke.BeginInvoke(Delegate method, object[] args)
        {
            if (HWND != null)
                return HWND.BeginInvoke(method, args);
            return null;
        }

        object ISynchronizeInvoke.EndInvoke(IAsyncResult result)
        {
            if (HWND != null)
                return HWND.EndInvoke(result);
            return null;
        }

        object ISynchronizeInvoke.Invoke(Delegate method, object[] args)
        {
            if (HWND != null)
                return HWND.Invoke(method, args);
            return null;
        }

        bool ISynchronizeInvoke.InvokeRequired
        {
            get { return HWND != null && HWND.InvokeRequired; }
        }

        #endregion
       
    }
}
